package com.dhcc.bpm.common.utils.database;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.dhcc.bpm.common.constant.FilePathConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Arrays;

/**
 * @program: cbus-bpm-boot
 * @ClassName ScrewExportDatabaseUtils
 * @description: TODO
 * @author: xiafayu
 * @create: 2021-11-15 15:00:34
 * @Version 1.0
 **/
@Component
public class ScrewExportDatabaseUtils {
    private static final Logger log = LoggerFactory.getLogger(ScrewExportDatabaseUtils.class);
    @Value("${bpm.file-public-path}")
    static String filePath;
    @Value("${bpm.dictData.version}")
    static String version;
    @Value("${bpm.dictData.description}")
    static String description;
    @Value("${bpm.dictData.fileName}")
    static String fileName;
    @Autowired
    ApplicationContext applicationContext;
    static Configuration config;
    static EngineConfig engineConfig;

    static {
        filePath = filePath + FilePathConstant.DICTFILE;
        log.info("生成数据字典路径:{}", filePath);
        // 生成文件配置
        engineConfig = EngineConfig.builder()
                .fileOutputDir(filePath)
                .openOutputDir(false)
                // 文件类型
                .fileType(EngineFileType.HTML)
                .fileName(fileName)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker).build();

    }

    @Async
    public void contextLoads() {
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
        // 生成文档配置（包含以下自定义版本号、描述等配置连接）
        config = Configuration.builder()
                .version(version)
                .description(description)
                .dataSource(dataSourceMysql)
                .engineConfig(engineConfig)
                .produceConfig(getProcessConfig(""))
                .build();
        // 执行生成
        new DocumentationExecute(config).execute();
    }

    public void createContextLoads(String tableNames) {
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
        // 生成文档配置（包含以下自定义版本号、描述等配置连接）
        config = Configuration.builder()
                .version(version)
                .description(description)
                .dataSource(dataSourceMysql)
                .engineConfig(engineConfig)
                .produceConfig(getProcessConfig(tableNames))
                .build();
        // 执行生成
        new DocumentationExecute(config).execute();
    }

    /**
     * 配置想要生成的表+ 配置想要忽略的表
     *
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig(String tableNames) {
        if (StringUtils.isNotBlank(tableNames)) {
            return ProcessConfig.builder()
                    //根据名称指定表生成
                    .designatedTableName(Arrays.asList(tableNames))
                    //根据表前缀生成
//                    .designatedTablePrefix(new ArrayList<>())
                    //根据表后缀生成
//                    .designatedTableSuffix(new ArrayList<>())
                    //忽略表名
//                .ignoreTableName(ignoreTableName)
                    //忽略表前缀
//                .ignoreTablePrefix(ignorePrefix)
                    //忽略表后缀
//                .ignoreTableSuffix(ignoreSuffix)
                    .build();
        }
        return ProcessConfig.builder().build();
    }
}
